﻿using System.Text;

namespace msilgc.cbuilder.CSyntaxTree
{
    internal class AssignToLocalVariableFromArray : ICStatementWithAssignment {
        public IVariable Target { get; set; }
        public IVariable SourceArray { get; set; }
        public IVariable SourceIndex { get; set; }
        public bool AddressOf { get; set; }
        public bool OnStack { get; set; }

        public AssignToLocalVariableFromArray(IVariable target, IVariable sourceArray, IVariable sourceIndex) {
            Target = target;
            SourceArray = sourceArray;
            SourceIndex = sourceIndex;
            OnStack = true;
            AddressOf = false;
        }

        public void Emit(StringBuilder builder) {
            Target.Emit(builder);
            builder.Append(" = ");
            if(AddressOf)
            {
                builder.Append("&(");
            }
            SourceArray.Emit(builder);
            builder.Append(".array_data[");
            SourceIndex.Emit(builder);
            builder.Append("]");
            if(AddressOf)
            {
                builder.Append(")");
            }
            builder.AppendLine(";");
        }
    }
}